package com.freesky.biz.rights.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freesky.biz.rights.domain.ArrearsDetailEntity;
import com.freesky.biz.rights.domain.EncryptBatchEntity;
import com.freesky.biz.rights.pojo.EncryptCodePo;
import com.freesky.biz.rights.service.IArrearsDetailService;
import com.freesky.biz.rights.service.IEncryptBatchService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.exception.base.BaseException;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.common.utils.StringUtils;
import com.freesky.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 补发明细管理（批量）
 */
@RestController
@RequestMapping("/rights/arrears/detail")
public class ArrearsDetailController extends BaseController {

    @Autowired
    private IArrearsDetailService arrearsDetailService;

    /**
     * 分页获取批次
     */
    @PreAuthorize("@ss.hasPermi('arrears:detail:list')")
    @GetMapping("/page")
    public TableDataInfo page(ArrearsDetailEntity entity) {
        IPage<ArrearsDetailEntity> pageList = arrearsDetailService.selectDetailPage(plusPage(),entity);
        return getDataTable(pageList);
    }

    /**
     * 根据id获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('arrears:detail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id) {
        return success(arrearsDetailService.getById(id));
    }

    /**
     * 删除券码批次
     */
    @PreAuthorize("@ss.hasPermi('arrears:detail:remove')")
    @Log(title = "补发明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        return toAjax(arrearsDetailService.deleteBatchByIds(ids.toArray(new String[0])));
    }

    @Log(title = "补发明细", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('arrears:detail:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArrearsDetailEntity entity) {
        if(StringUtils.isEmpty(entity.getBatchId())){
            throw new BaseException("批次号ID不能为空");
        }
        List<ArrearsDetailEntity> list = arrearsDetailService.selectDetailPage(entity);
        ExcelUtil<ArrearsDetailEntity> util = new ExcelUtil<ArrearsDetailEntity>(ArrearsDetailEntity.class);
        util.exportExcel(response, list, "补发明细");
    }


}
